import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class PictureExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Picture Example")),
      body: Center(
        child: CustomPaint(
          size: Size(300, 300), // Define the size of the canvas
          painter: PicturePainter(),
        ),
      ),
    );
  }
}

class PicturePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // 1. Create a PictureRecorder to start recording drawing commands
    final recorder = ui.PictureRecorder();
    final canvasForPicture = Canvas(recorder, Rect.fromPoints(Offset(0, 0), Offset(size.width, size.height)));

    // 2. Draw on the canvas
    final paint = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.fill;

    // 1. 绘制矩形
    canvasForPicture.drawRect(Rect.fromLTWH(50, 50, 200, 200), paint);

    // 2. 绘制圆形
    paint.color = Colors.red; // 设置红色
    canvasForPicture.drawCircle(Offset(300, 150), 100, paint); // 在指定位置绘制半径为100的圆形

    // 3. 绘制椭圆
    paint.color = Colors.green; // 设置绿色
    canvasForPicture.drawOval(Rect.fromLTWH(500, 50, 150, 100), paint); // 绘制椭圆，设置矩形区域

    // 4. 绘制路径 (自定义形状：五角星)
    final path = Path()
      ..moveTo(0, 0) // 起点
      ..lineTo(100, 100) // 绘制到(450, 400)
      ..lineTo(200, 100) // 绘制到(350, 350)
      ..lineTo(50, 50) // 绘制到(450, 350)
      // ..lineTo(350, 400) // 绘制到(350, 400)
      ..close(); // 关闭路径，形成五角星
    // paint.color = Colors.orange; // 设置为橙色
    canvasForPicture.drawPath(path, paint); // 绘制路径

    // 3. End the recording and create the Picture
    final picture = recorder.endRecording();

    // 4. Use the Picture to draw onto the original canvas
    canvas.drawPicture(picture);

  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

void main() {
  runApp(MaterialApp(home: PictureExample()));
}
